package com.oracle.controller;

import com.oracle.admin.service.api.AdminServiceApi;
import com.oracle.annotation.Action;
import com.oracle.annotation.ActionType;
import com.oracle.pojo.Admins;
import com.oracle.pojo.Roles;
import com.oracle.role.service.api.RoleServiceApi;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired(required = false)
    private AdminServiceApi adminServiceApi;
    @Autowired(required = false)
    private RoleServiceApi roleServiceApi;


    @Action(ActionType.QUERY)
    @RequestMapping("/list")
    public String adminList(Model model){
        ResultEntity<List<Admins>> resultEntity = adminServiceApi.findAdminsAll();
        model.addAttribute("adminList",resultEntity.getData());
        return "admin/admin/list";
    }


    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/addview")
    public String adminAddView(Model model){
        ResultEntity<List<Roles>> resultEntity = this.roleServiceApi.findRoleAll();
        model.addAttribute("roleList",resultEntity.getData());
        return "admin/admin/addview";
    }

    @Action(ActionType.ADD)
    @RequestMapping("/add")
    public String adminAdd(Admins admins, Integer...roleids){
        //编写管理员添加业务
        this.adminServiceApi.addAdminAndRoles(admins,roleids);
        return "admin/comm/success";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/editview")
    public String adminEditView(Integer id, Model model){
        //编写查找指定管理员信息的业务
        ResultEntity<Admins> adminResultEntity = this.adminServiceApi.findAdminById(id);
        ResultEntity<List<Roles>> roleResultEntity = this.roleServiceApi.findRoleAll();
        ResultEntity<List<Integer>> rolesByAdminResult = this.adminServiceApi.getRolesByAdminId(id);
        model.addAttribute("roleList",roleResultEntity.getData());
        model.addAttribute("admin",adminResultEntity.getData());
        model.addAttribute("adminRoles",rolesByAdminResult.getData());
        return "admin/admin/editview";
    }

    @Action(ActionType.UPDATE)
    @RequestMapping("/edit")
    public String adminEdit(Admins admin,Integer...roleids){
        //编写修改指定管理员信息的业务
        ResultEntity resultEntity = this.adminServiceApi.updateAdminAndRoles(admin,roleids);
        return "admin/comm/success";
    }

    @Action(ActionType.DELETE)
    @RequestMapping("/delete")
    @ResponseBody
    public Map adminDelete(Integer id, Model model){
        Map<String,Object> resultMaps = new HashMap<>();
        //编写删除指定管理员信息的业务
        ResultEntity<Admins> resultEntity = this.adminServiceApi.deleteAdminById(id);
        resultMaps.put("code",resultEntity.getCode());
        resultMaps.put("msg",resultEntity.getMsg());
        return resultMaps;
    }


}
